const mysql = require('mysql2/promise');
const config = require('./config');
const logger = require('../utils/logger');

// 创建连接池
const pool = mysql.createPool(config.database);

// 测试数据库连接
pool.getConnection()
  .then(connection => {
    logger.success('数据库连接成功', {
      host: config.database.host,
      database: config.database.database
    });
    connection.release();
  })
  .catch(err => {
    logger.error('数据库连接失败', {
      error: err.message,
      host: config.database.host,
      database: config.database.database
    });
  });

// 连接池错误处理
pool.on('error', (err) => {
  logger.error('数据库连接池错误', { error: err.message });
});

module.exports = pool;


